package io.netty.test.main.bio;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/**
 * @author LuoYu
 * @date 2018/10/27 13:08
 * <p>
 * Description:
 * socket客户端接入
 */
public class ClientHandler {
    public static final int MAX_DATA_LEN = 1024;
    private final Socket client;

    public ClientHandler(Socket client) {
        this.client = client;
    }

    public void start(){
        System.out.println("new client in");
        new Thread(() -> handle()).start();
    }

    private void handle(){
        try {
            System.out.println("current handle thread:" + Thread.currentThread().getName());
            InputStream inputStream = client.getInputStream();
            while (true){
                byte[] data = new byte[MAX_DATA_LEN];
                int len;
                while((len = inputStream.read(data)) != -1){
                    String message = new String(data,0,len);
                    System.out.println("client line message:" + message);
                    client.getOutputStream().write(("ack:"+message).getBytes());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




}
